package com.byzoro.service;

import com.byzoro.mapper.ModelsMapper;
import com.byzoro.pojo.Models;
import com.byzoro.utils.JsonResult;
import com.byzoro.utils.Page;
import com.byzoro.utils.RestfulMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ModelsService {
    @Autowired
    ModelsMapper modelsMapper;

    public JsonResult list (int page, int limit) {
        if (page <= 0) page = 1;
        if (limit <= 1) limit = 1;
        int offset = Page.page(page, limit);
        List<Models> data = modelsMapper.list(offset, limit);
        int count = modelsMapper.count();
        return JsonResult.list("成功", data, count);
    }

    public JsonResult create (Models models) {
        int row = modelsMapper.create(models);
        if (row > 0) {
            return JsonResult.success(RestfulMessage.OpOk);
        }
        return JsonResult.fail(RestfulMessage.OpFail);
    }

    public JsonResult update (Models models) {
        int row = modelsMapper.update(models);
        if (row >= 0) {
            return JsonResult.success(RestfulMessage.OpOk);
        }
        return JsonResult.fail(RestfulMessage.OpFail);
    }

    public JsonResult delete (int id) {
        int row = modelsMapper.delete(id);
        if (row >= 0) {
            return JsonResult.success(RestfulMessage.OpOk);
        }
        return JsonResult.fail(RestfulMessage.OpFail);
    }

    public JsonResult detail (int id) {
        Models model = modelsMapper.detail(id);
        if (model != null) {
            return JsonResult.success(RestfulMessage.OpOk, model);
        }
        return JsonResult.fail(RestfulMessage.OpFail);
    }
}
